package com.comdo.newup_loan.module.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.comdo.newup_loan.MainActivity;
import com.comdo.newup_loan.R;
import com.comdo.newup_loan.base.BaseActivity;
import com.comdo.newup_loan.comom.Config;
import com.comdo.newup_loan.model.LoginInfo;
import com.comdo.newup_loan.module.register.FindPassActivity;
import com.comdo.newup_loan.module.register.RegisterActivity;
import com.comdo.newup_loan.util.ACache;
import com.comdo.newup_loan.util.StringUtils;

import butterknife.BindView;

/**
 * Created by liyuan on 2017/10/30.
 */

public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.View ,View.OnClickListener{
    @BindView(R.id.ed_username)
    EditText nameEditText;
    @BindView(R.id.ed_password)
    EditText passEditText;
    @BindView(R.id.bt_login)
    Button loginButton;
    @BindView(R.id.to_register)
    TextView registerTextView;
    @BindView(R.id.to_findpass)
    TextView findPassTextView;
    @Override
    protected void init() {
        super.init();
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_login;
    }

    @Override
    public LoginPresenter getmPresenter() {
        return new LoginPresenter();
    }

    @Override
    public void presenterSetView() {
        if (mPresenter != null)
            mPresenter.setView(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        loginButton.setOnClickListener(this);
        registerTextView.setOnClickListener(this);
        findPassTextView.setOnClickListener(this);
    }

    @Override
    public void loginFail(String failMessage) {
        showToast(failMessage);
    }

    @Override
    public void getLoginInfo(LoginInfo loginInfo) {

        Config.loginInfo=loginInfo;
        ACache.get(LoginActivity.this).put("loginInfo",loginInfo);
        toMainActivity();
        finish();
    }
    private void toMainActivity() {
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
    }
    private void toRegisterActivity() {
        Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
        startActivity(intent);
    }
    private void toFindPassActivity() {
        Intent intent = new Intent(LoginActivity.this, FindPassActivity.class);
        startActivity(intent);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_login:
                String userName=nameEditText.getText().toString();
                String passWord=passEditText.getText().toString();
                if(StringUtils.isNull(userName)){
                    Toast.makeText(getApplicationContext(), "用户名不能为空!", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(StringUtils.isNull(passWord)){
                    Toast.makeText(getApplicationContext(), "密码不能为空!", Toast.LENGTH_SHORT).show();
                    return;
                }
                mPresenter.login(userName,passWord);
                break;
            case R.id.to_register:
                toRegisterActivity();
                break;
            case R.id.to_findpass:
                toFindPassActivity();
                break;
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            LoginActivity.this.finish();
            Intent intent=new Intent(LoginActivity.this,MainActivity.class);
            intent.putExtra("notLogin", "notLogin");
            startActivity(intent);
        }
        return true;
    }

}
